package com.keye.model.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.keye.common.servlet.MyInternalResourceViewResolver;
import com.keye.model.domain.User;

@Controller
@RequestMapping("/user")
public class UserController {

	
	@RequestMapping("/login")
	public ModelAndView login(User user,ModelAndView modelAndView) {
		Subject subject=SecurityUtils.getSubject();
		UsernamePasswordToken token=new UsernamePasswordToken(user.getUserName(), user.getPassword());
		try{
			subject.login(token);
			Session session=subject.getSession();
			System.out.println("sessionId:"+session.getId());
			System.out.println("sessionHost:"+session.getHost());
			System.out.println("sessionTimeout:"+session.getTimeout());
			session.setAttribute("info", "session的数据");
			modelAndView.addObject("user", user);
			modelAndView.setViewName("success");
		}catch(Exception e){
			e.printStackTrace();
			modelAndView.addObject("user", user);
			modelAndView.addObject("errorMsg", user.getUserName()+":用户名或密码错误！");
			modelAndView.setViewName(MyInternalResourceViewResolver.NOT_URL_PREFIX+"/index.jsp");
		}
		return modelAndView;
	}
	
	@RequestMapping("/select")
	public ModelAndView select(ModelAndView modelAndView) {
		modelAndView.setViewName("select");
		return modelAndView;
	}
}
